Option Explicit On Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form Dim songsRated(200) As String Dim ratings(200) As Integer Dim numTimesPlayed(200) As Integer Dim songNum As Integer Dim lastSongPlayed As Integer #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TxtSong As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents TxtRating As System.Windows.Forms.TextBox Friend WithEvents LstSongs As System.Windows.Forms.ListBox Friend WithEvents btnShowHigh As System.Windows.Forms.Button Friend WithEvents btnAve As System.Windows.Forms.Button Friend WithEvents BtnFindRating As System.Windows.Forms.Button Friend WithEvents ButHigh As System.Windows.Forms.Button Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents BtnNumAbove As System.Windows.Forms.Button Friend WithEvents TxtCurrRating As System.Windows.Forms.TextBox Friend WithEvents TxtCurrSong As System.Windows.Forms.TextBox Friend WithEvents BtnPlayNext As System.Windows.Forms.Button Friend WithEvents BtnSim As System.Windows.Forms.Button Friend WithEvents BtnShow As System.Windows.Forms.Button Private Sub InitializeComponent() Me.LstSongs = New System.Windows.Forms.ListBox Me.Label1 = New System.Windows.Forms.Label Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.TxtCurrRating = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.TxtCurrSong = New System.Windows.Forms.TextBox Me.Label5 = New System.Windows.Forms.Label Me.Button2 = New System.Windows.Forms.Button Me.TxtRating = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.TxtSong = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.btnShowHigh = New System.Windows.Forms.Button Me.btnAve = New System.Windows.Forms.Button Me.BtnFindRating = New System.Windows.Forms.Button Me.ButHigh = New System.Windows.Forms.Button Me.BtnNumAbove = New System.Windows.Forms.Button Me.BtnPlayNext = New System.Windows.Forms.Button Me.BtnSim = New System.Windows.Forms.Button Me.BtnShow = New System.Windows.Forms.Button Me.GroupBox1.SuspendLayout() Me.SuspendLayout() ' 'LstSongs ' Me.LstSongs.Location = New System.Drawing.Point(7, 42) Me.LstSongs.Name = "LstSongs" Me.LstSongs.Size = New System.Drawing.Size(273, 407) Me.LstSongs.TabIndex = 0 ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(7, 14) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(133, 28) Me.Label1.TabIndex = 1 Me.Label1.Text = "Songs" ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.BtnShow) Me.GroupBox1.Controls.Add(Me.BtnSim) Me.GroupBox1.Controls.Add(Me.BtnPlayNext) Me.GroupBox1.Controls.Add(Me.TxtCurrRating) Me.GroupBox1.Controls.Add(Me.Label4) Me.GroupBox1.Controls.Add(Me.TxtCurrSong) Me.GroupBox1.Controls.Add(Me.Label5) Me.GroupBox1.Location = New System.Drawing.Point(287, 42) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(313, 201) Me.GroupBox1.TabIndex = 2 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Playing Song" ' 'TxtCurrRating ' Me.TxtCurrRating.Location = New System.Drawing.Point(96, 80) Me.TxtCurrRating.Name = "TxtCurrRating" Me.TxtCurrRating.ReadOnly = True Me.TxtCurrRating.Size = New System.Drawing.Size(200, 20) Me.TxtCurrRating.TabIndex = 7 Me.TxtCurrRating.Text = "" ' 'Label4 ' Me.Label4.Location = New System.Drawing.Point(8, 80) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(86, 28) Me.Label4.TabIndex = 6 Me.Label4.Text = "Rating (0-5)" ' 'TxtCurrSong ' Me.TxtCurrSong.Location = New System.Drawing.Point(96, 40) Me.TxtCurrSong.Name = "TxtCurrSong" Me.TxtCurrSong.ReadOnly = True Me.TxtCurrSong.Size = New System.Drawing.Size(200, 20) Me.TxtCurrSong.TabIndex = 5 Me.TxtCurrSong.Text = "" ' 'Label5 ' Me.Label5.Location = New System.Drawing.Point(16, 40) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(47, 20) Me.Label5.TabIndex = 4 Me.Label5.Text = "Song" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(528, 424) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(73, 27) Me.Button2.TabIndex = 6 Me.Button2.Text = "Quit" ' 'TxtRating ' Me.TxtRating.Location = New System.Drawing.Point(392, 384) Me.TxtRating.Name = "TxtRating" Me.TxtRating.Size = New System.Drawing.Size(160, 20) Me.TxtRating.TabIndex = 3 Me.TxtRating.Text = "" ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(304, 384) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(86, 28) Me.Label3.TabIndex = 2 Me.Label3.Text = "Rating (0-5)" ' 'TxtSong ' Me.TxtSong.Location = New System.Drawing.Point(392, 344) Me.TxtSong.Name = "TxtSong" Me.TxtSong.Size = New System.Drawing.Size(160, 20) Me.TxtSong.TabIndex = 1 Me.TxtSong.Text = "" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(312, 344) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(47, 20) Me.Label2.TabIndex = 0 Me.Label2.Text = "Song" ' 'btnShowHigh ' Me.btnShowHigh.Location = New System.Drawing.Point(288, 264) Me.btnShowHigh.Name = "btnShowHigh" Me.btnShowHigh.Size = New System.Drawing.Size(72, 32) Me.btnShowHigh.TabIndex = 3 Me.btnShowHigh.Text = "Show High" ' 'btnAve ' Me.btnAve.Location = New System.Drawing.Point(368, 264) Me.btnAve.Name = "btnAve" Me.btnAve.Size = New System.Drawing.Size(80, 32) Me.btnAve.TabIndex = 4 Me.btnAve.Text = "Ave Rating" ' 'BtnFindRating ' Me.BtnFindRating.Location = New System.Drawing.Point(304, 416) Me.BtnFindRating.Name = "BtnFindRating" Me.BtnFindRating.Size = New System.Drawing.Size(80, 32) Me.BtnFindRating.TabIndex = 5 Me.BtnFindRating.Text = "Find Rating" ' 'ButHigh ' Me.ButHigh.Location = New System.Drawing.Point(288, 304) Me.ButHigh.Name = "ButHigh" Me.ButHigh.Size = New System.Drawing.Size(72, 32) Me.ButHigh.TabIndex = 6 Me.ButHigh.Text = " Find Highest" ' 'BtnNumAbove ' Me.BtnNumAbove.Location = New System.Drawing.Point(368, 304) Me.BtnNumAbove.Name = "BtnNumAbove" Me.BtnNumAbove.Size = New System.Drawing.Size(80, 32) Me.BtnNumAbove.TabIndex = 7 Me.BtnNumAbove.Text = "Num Above Ave" ' 'BtnPlayNext ' Me.BtnPlayNext.Location = New System.Drawing.Point(16, 120) Me.BtnPlayNext.Name = "BtnPlayNext" Me.BtnPlayNext.Size = New System.Drawing.Size(88, 24) Me.BtnPlayNext.TabIndex = 8 Me.BtnPlayNext.Text = "Play Next" ' 'BtnSim ' Me.BtnSim.Location = New System.Drawing.Point(120, 120) Me.BtnSim.Name = "BtnSim" Me.BtnSim.Size = New System.Drawing.Size(96, 24) Me.BtnSim.TabIndex = 9 Me.BtnSim.Text = "Simulate Play" ' 'BtnShow ' Me.BtnShow.Location = New System.Drawing.Point(224, 120) Me.BtnShow.Name = "BtnShow" Me.BtnShow.Size = New System.Drawing.Size(80, 24) Me.BtnShow.TabIndex = 10 Me.BtnShow.Text = "Show Played" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(606, 465) Me.Controls.Add(Me.BtnNumAbove) Me.Controls.Add(Me.ButHigh) Me.Controls.Add(Me.BtnFindRating) Me.Controls.Add(Me.btnAve) Me.Controls.Add(Me.btnShowHigh) Me.Controls.Add(Me.GroupBox1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.LstSongs) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.TxtRating) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.TxtSong) Me.Controls.Add(Me.Label2) Me.Name = "Form1" Me.Text = "Form1" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub #End Region ' random whether to play or not - 5 is 5/6 likely to play, 4 is 4/6 ... Private Function play(ByVal rating As Integer) As Boolean Dim rand As Integer ' generates random value between 0 and 5, then adds 1 rand = Convert.ToInt32(5 * Rnd() + 1) ' if random number is greater than rating - don't play If rand > rating Then Return False Else Return True End If End Function Private Function chooseNextSongToPlay() As Integer ' first move to next song lastSongPlayed = lastSongPlayed + 1 If lastSongPlayed >= songNum Then ' need to loop back around lastSongPlayed = 0 End If Dim played As Boolean = False ' loop until find one to play Do Until played ' see if should play this one played = play(ratings(lastSongPlayed)) If played Then ' keep track of how many played numTimesPlayed(lastSongPlayed) = numTimesPlayed(lastSongPlayed) + 1 Else ' not playing - try next lastSongPlayed = lastSongPlayed + 1 If lastSongPlayed >= songNum Then ' need to loop back around lastSongPlayed = 0 End If End If Loop Return lastSongPlayed End Function Private Function averageArray(ByVal array() As Integer, ByVal num As Integer) As Double Dim total As Integer Dim cnt As Integer For cnt = 0 To num - 1 total = total + array(cnt) Next cnt Dim ave As Double ave = total / cnt Return ave End Function Private Function averageRating(ByVal array() As Integer) As Double Dim total As Integer Dim cnt As Integer For cnt = 0 To songNum - 1 total = total + array(cnt) Next cnt Dim ave As Double ave = total / cnt Return ave End Function Private Function howManyAboveN(ByVal array() As Integer, ByVal thresh As Double) As Integer Dim howMany As Integer Dim cnt As Integer For cnt = 0 To songNum - 1 If array(cnt) > thresh Then howMany = howMany + 1 End If Next cnt Return howMany End Function Private Function checkRating(ByRef rating As Integer) As Boolean If IsNumeric(TxtRating.Text) Then rating = Convert.ToInt32(TxtRating.Text) If rating < 0 Or rating > 5 Then MsgBox("Rating must be between 0 and 5") Return False Else Return True End If Else MsgBox("Rating must be numeric") Return False End If End Function Private Sub clear() TxtSong.Text = "" TxtRating.Text = "" End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) clear() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim input As IO.StreamReader If IO.File.Exists("results.txt") Then input = IO.File.OpenText("results.txt") Do While input.Peek <> -1 And songNum <= 200 Dim curr As String curr = input.ReadLine() Dim commapos As Integer commapos = curr.IndexOf(",") songsRated(songNum) = curr.Substring(0, commapos) ratings(songNum) = Convert.ToInt32(curr.Substring(commapos + 1)) songNum = songNum + 1 Loop End If input.Close() ' show all elements of the array Dim cnt As Integer Dim results As String = "" For cnt = 0 To songNum - 1 LstSongs.Items.Add(songsRated(cnt) & " Rated: " & ratings(cnt)) Next cnt lastSongPlayed = -1 End Sub Private Sub LstSongs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstSongs.SelectedIndexChanged If LstSongs.SelectedItem Is Nothing Then Else TxtSong.Text = Convert.ToString(LstSongs.SelectedItem) End If End Sub Private Sub BtnRate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim outp As IO.StreamWriter Dim ok As Boolean Dim rating As Integer ok = checkRating(rating) If TxtSong.Text <> "" And ok Then ' everything is ok outp = IO.File.AppendText("results.txt") outp.WriteLine(TxtSong.Text & ", " & rating) outp.Close() MsgBox("you have selected " & TxtSong.Text & " and rated it: " & rating) ' remove the song from the list box LstSongs.Items.Remove(TxtSong.Text) ' clear song and rating clear() End If End Sub Private Sub BtnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub btnShowHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowHigh.Click Dim cnt As Integer For cnt = 0 To songNum - 1 If ratings(cnt) >= 4 Then MsgBox(songsRated(cnt) & " Rated: " & ratings(cnt)) End If Next cnt End Sub Private Sub btnAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAve.Click Dim ave As Double 'ave = averageRating(ratings) ave = averageArray(ratings, songNum) MsgBox("ave rating is: " & ave.ToString("n2")) End Sub Private Sub BtnFindRating_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFindRating.Click Dim found As Integer = -1 Dim cnt As Integer For cnt = 0 To songNum - 1 If songsRated(cnt) = TxtSong.Text Then found = cnt End If Next cnt If found = -1 Then MsgBox("Song : " & TxtSong.Text & " not found") Else TxtRating.Text = ratings(found).ToString() End If End Sub Private Sub ButHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButHigh.Click Dim cnt As Integer Dim highest As Integer = -1 Dim found As Integer = -1 For cnt = 0 To songNum - 1 If ratings(cnt) > highest Then ' we have a new highest highest = ratings(cnt) found = cnt End If Next cnt MsgBox("Highest Rated song is: " & songsRated(found) & " with a rating of " & highest) End Sub Private Sub BtnNumAbove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNumAbove.Click Dim ave As Double ave = averageRating(ratings) Dim high As Integer high = howManyAboveN(ratings, ave) MsgBox(" " & high & " songs above ave: " & ave) End Sub Private Sub BtnPlayNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPlayNext.Click Dim play As Integer play = chooseNextSongToPlay() TxtCurrSong.Text = songsRated(play) TxtCurrRating.Text = ratings(play).ToString("n0") End Sub Private Sub BtnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShow.Click Dim results As String = "" Dim cnt As Integer For cnt = 0 To songNum - 1 results = results & songsRated(cnt) & " rated: " & ratings(cnt) & " played: " & numTimesPlayed(cnt) & Environment.NewLine Next cnt MsgBox(results) End Sub Private Sub BtnSim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSim.Click Dim cnt As Integer = 0 For cnt = 0 To 10000 Dim play As Integer play = chooseNextSongToPlay() Next MsgBox("simulation done") End Sub End Class